package cn.xutongxue.gtms.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.xutongxue.gtms.base.Errors;
import cn.xutongxue.gtms.base.Result;
import cn.xutongxue.gtms.constant.UserConstant;
import cn.xutongxue.gtms.entity.User;
import cn.xutongxue.gtms.exception.BusinessException;
import cn.xutongxue.gtms.exception.ThrowUtils;
import cn.xutongxue.gtms.service.impl.UserServiceImpl;
import cn.xutongxue.gtms.utils.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author XuTongXue
 * @version 1.0
 * @date 2023/3/5 14:18
 * @Description
 */

@Slf4j
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员Controller")
@SaCheckRole(UserConstant.ADMIN_ROLE)
@CrossOrigin
public class AdminController {

    @Resource
    private UserServiceImpl userService;

    @GetMapping("/getAllTeacher")
    @ApiOperation("获取教师列表")
    public Result getAllTeacher(@RequestParam("current") Integer current,
                                @RequestParam("size") Integer size) {
        ThrowUtils.throwIf(size > 100, Errors.PARAMS_ERROR);
        Page<User> teachers = userService.getAllTeacher(current, size);
        return Result.success(teachers);
    }

    @PostMapping("/addTeacher")
    @ApiOperation("添加教师")
    public Result addTeacher(@RequestBody User user) {
        if (user == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        if (user.getId() == null) {
            user.setAvatar(UserConstant.DEFAULT_AVATAR);
            user.setPassword(UserConstant.DEFAULT_PASSWORD);
            user.setState(0);
            user.setIsDeleted(0);
            user.setRole(UserConstant.TEACHER_ROLE);
            Long adminId = StpUtil.getLoginIdAsLong();
            User admin = userService.getById(adminId);
            user.setCreator(admin.getUsername());
            ThrowUtils.throwIf(!userService.save(user), Errors.OPERATION_ERROR);
        } else {
            ThrowUtils.throwIf(!userService.updateById(user), Errors.OPERATION_ERROR);
        }
        return Result.success(user);
    }

    @DeleteMapping("/deleteTeacher")
    @ApiOperation("删除教师账号")
    public Result deleteTeacher(@RequestParam("teacherId") Long teacherId) {
        if (teacherId == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        ThrowUtils.throwIf(!userService.removeById(teacherId), Errors.OPERATION_ERROR);
        return Result.success(true);
    }

    @PostMapping("/importTeacher")
    @ApiOperation("导入教师账号")
    public void importTeacher(@RequestBody MultipartFile file) {
        new ExcelUtil(userService).importToDatabase(file, User.class);
    }

    @PostMapping("/exportTeacher")
    @ApiOperation("导出教师账号")
    public void exportTeacher(HttpServletResponse response) {
        new ExcelUtil(userService).exportToDownload(response, "教师账号", User.class, new LambdaQueryWrapper<User>().eq(User::getRole, UserConstant.TEACHER_ROLE));
    }


    @GetMapping("/getAllStudent")
    @ApiOperation("获取学生列表")
    public Result getAllStudent(@RequestParam("current") Integer current,
                                @RequestParam("size") Integer size) {
        ThrowUtils.throwIf(size > 100, Errors.PARAMS_ERROR);
        Page<User> students = userService.getAllStudent(current, size);
        return Result.success(students);
    }

    @PostMapping("/addStudent")
    @ApiOperation("添加学生")
    public Result addStudent(@RequestBody User user) {
        if (user == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        if (user.getId() == null) {
            user.setAvatar(UserConstant.DEFAULT_AVATAR);
            user.setPassword(UserConstant.DEFAULT_PASSWORD);
            user.setState(0);
            user.setIsDeleted(0);
            user.setRole(UserConstant.DEFAULT_ROLE);
            Long adminId = StpUtil.getLoginIdAsLong();
            User admin = userService.getById(adminId);
            user.setCreator(admin.getUsername());
            ThrowUtils.throwIf(!userService.save(user), Errors.OPERATION_ERROR);
        } else {
            ThrowUtils.throwIf(!userService.updateById(user), Errors.OPERATION_ERROR);
        }
        return Result.success(user);
    }

    @DeleteMapping("/deleteStudent")
    @ApiOperation("删除学生账号")
    public Result deleteStudent(@RequestParam("studentId") Long studentId) {
        if (studentId == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        ThrowUtils.throwIf(!userService.removeById(studentId), Errors.OPERATION_ERROR);
        return Result.success(true);
    }

    @PostMapping("/importStudent")
    @ApiOperation("导入学生账号")
    public void importStudent(@RequestBody MultipartFile file) {
        new ExcelUtil(userService).importToDatabaseStudent(file, User.class);
    }

    @PostMapping("/exportStudent")
    @ApiOperation("导出学生账号")
    public void exportStudent(HttpServletResponse response) {
        new ExcelUtil(userService).exportToDownload(response, "学生账号", User.class, new LambdaQueryWrapper<User>().eq(User::getRole, UserConstant.DEFAULT_ROLE));
    }


    @PutMapping("/resetPassword")
    @ApiOperation("重置密码")
    public Result resetPassword(@RequestParam("userId") Long userId) {
        if (userId == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        User user = userService.getById(userId);
        user.setPassword(UserConstant.DEFAULT_PASSWORD);
        ThrowUtils.throwIf(!userService.updateById(user), Errors.OPERATION_ERROR);
        return Result.success(true);
    }
}
